/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.RawMaterialPurchase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import merlion.common.util.Consts;
import merlion.scm.rmpm.entity.RawMaterial;
import merlion.scm.rmpm.entity.Supplier;
import merlion.scm.rmpm.pojo.SupplierListRow;
import merlion.scm.rmpm.session.RawMaterialPurchaseSessionBeanLocal;

/**
 *
 * @author Kenn
 */
@ManagedBean
@ViewScoped
public class SupplierListManagerBean {

    @EJB
    private RawMaterialPurchaseSessionBeanLocal rmpSessionBean;
    
    private Supplier supplier;
    
    //--------------------------------------------------------------------------
    //                          Constructor
    //--------------------------------------------------------------------------
    public SupplierListManagerBean() {
        
        supplier = (Supplier)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("supplier");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("supplier");
        
        if(supplier == null){
            supplier = new Supplier();
        }
        
    }
    
    //--------------------------------------------------------------------------
    //                          Manager Bean Methods
    //--------------------------------------------------------------------------
    public List<Supplier> getAllSupplier(){
        return rmpSessionBean.getAllSupplier();
    }
    
    public List<SupplierListRow> getSupplierListRow(){
        ArrayList<Supplier> suppliers = new ArrayList<Supplier>(getAllSupplier());
        
        ArrayList<RawMaterial> rmObjects = new ArrayList<RawMaterial>();
        for(String s: Consts.allRawMaterialNames){
            rmObjects.add(rmpSessionBean.getRawMaterialbyName(s));
        }
        
        ArrayList<SupplierListRow> supplierListRows = new ArrayList<SupplierListRow> ();
        for(Supplier s: suppliers){
            SupplierListRow slr = new SupplierListRow(s, rmObjects);
            supplierListRows.add(slr);
        }
        return supplierListRows;
    }
    
    public String viewSupplier(Long id){
        supplier = rmpSessionBean.getSupplierById(id);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("supplier", supplier);
        
        return "viewSupplierListDisplay?faces-redirect=true";
    }
    
    public String editSupplier(Long id){
        supplier = rmpSessionBean.getSupplierById(id);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("supplier", supplier);
        
        return "viewSupplierListEdit?faces-redirect=true";
    }
    
    public String viewSupplierList(){
        return "viewSupplierList?faces-redirect=true";
    }
    
    //--------------------------------------------------------------------------
    //                           Getter and Setter
    //--------------------------------------------------------------------------

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }
    
}
